package com.netease.financial.common.util;

import android.content.Context;

import com.netease.financial.common.constant.Constants;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author yufeng.wu
 */
public class HttpUtils {
    private static final String TAG = "HttpUtils";

    private static final int CONNECTION_SUCCESS_CODE = 200;

    private static boolean isConnectionSuccess(int code) {
        return code == CONNECTION_SUCCESS_CODE;
    }

    public static String getExtraHeader(Context context) {
        return Constants.HTTP_EXTRA_HEADER_PREFIX + "/" + PackageUtils.getCurrentApkVersionName(context);
    }

    public static HttpURLConnection getHttpUrlConnection(String strUrl) throws IOException {
        URL url = new URL(strUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        return conn;
    }

    public static InputStream getInputStream(String strUrl) {
        NfsLog.d(TAG, "strUrl = " + strUrl);

        InputStream is = null;
        try {
            HttpURLConnection conn = HttpUtils.getHttpUrlConnection(strUrl);
            if (HttpUtils.isConnectionSuccess(conn.getResponseCode())) {
                is = conn.getInputStream();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return is;
    }

}
